package com.ganzalang.gmall.promotion.service.impl;

import com.ganzalang.gmall.promotion.dto.*;
import com.ganzalang.gmall.promotion.service.PromotionService;
import com.ganzalang.gmall.promotion.service.RuleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author Ganzalang
 * @description 促销活动的实现类
 * @Date 2022/4/3 23:09
 */
@Service
@RequiredArgsConstructor
public class PromotionServiceImpl implements PromotionService {

    private final RuleService ruleService;

    /**
     * 处理
     *
     * @param context 购物车上下文
     * @return
     */
    @Override
    public PromotionResult handle(PromotionContext context) {
        PromotionResult result = new PromotionResult();

        // 根据商品ID和活动id再去后端（缓存、DB）查一次促销活动的详情，如果查不到则查询全场范围的活动，此处暂时不实现
        DiscountDTO discountDTO = context.getCartPromotionItem().getDiscountDTO();

        RuleContext ruleContext = new RuleContext();
        ruleContext.setCartPromotionItem(context.getCartPromotionItem());
        RuleResult ruleResult = ruleService.validateRule(ruleContext);
        result.setRuleResult(ruleResult);

        return result;
    }
}
